<template>
    <div class="flow-process-dialog-container">
        <el-dialog :title="state.dialog.title" v-model="state.dialog.isShowDialog" width="769px">
            <el-form ref="processDialogFormRef" :model="state.ruleForm" :rules="state.rules" size="default" label-width="90px">
                <el-row :gutter="35">
                    <el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
                            <el-input v-model="state.ruleForm.content" input-style="height: 300px;" type="textarea" placeholder="请输入流程模型定义" clearable></el-input>
                    </el-col>
                </el-row>
            </el-form>
            <template #footer>
				<span class="dialog-footer">
					<el-button @click="onCancel" size="default">取 消</el-button>
					<el-button type="primary" @click="onSubmit" size="default">{{ state.dialog.submitTxt }}</el-button>
				</span>
            </template>
        </el-dialog>
    </div>
</template>

<script setup lang="ts" name="flowProcessDialog">
    import { reactive, ref, nextTick } from 'vue';
    import constConfig from '/@/utils/constConfig';
    import { processApi } from '/@/api/flow/process';

    // 定义子组件向父组件传值/事件
    const emit = defineEmits(['refresh']);

    // 定义变量内容
    const processDialogFormRef = ref();
    const state = reactive({
        ruleForm: {} as ProcessType,
        dialog: {
            isShowDialog: false,
            type: '',
            title: '',
            submitTxt: '',
        },
        rules: {
            id:{ required: true, message: "主键ID不能为空", trigger: "blur" },
            name:{ required: true, message: "流程名称不能为空", trigger: "blur" },
            displayName:{ required: true, message: "流程显示名称不能为空", trigger: "blur" },
            type:{ required: true, message: "流程类型不能为空", trigger: "blur" },
            instanceUrl:{ required: true, message: "实例url不能为空", trigger: "blur" },
            state:{ required: true, message: "流程是否可用不能为空", trigger: "blur" },
            content:{ required: true, message: "流程模型定义不能为空", trigger: "blur" },
            version:{ required: true, message: "版本不能为空", trigger: "blur" },
            createTime:{ required: true, message: "创建时间不能为空", trigger: "blur" },
            creator:{ required: true, message: "创建人不能为空", trigger: "blur" },
        },
    });

    // 打开弹窗
    const openDialog = (type: string, row: ProcessType) => {
        state.dialog.type = type;
        state.dialog.isShowDialog = true;
        state.ruleForm = row;
        if (type === constConfig.dialogType.add) {
            state.dialog.title = '导入流程定义表';
            state.dialog.submitTxt = '新 增';
        } else {
            state.dialog.title = '修改流程定义表';
            state.dialog.submitTxt = '修 改';
        }
        nextTick(() => {
            processDialogFormRef.value.clearValidate();
        });
    };
    // 关闭弹窗
    const closeDialog = () => {
        state.dialog.isShowDialog = false;
    };
    // 取消
    const onCancel = () => {
        closeDialog();
    };
    // 提交
    const onSubmit = () => {
        processDialogFormRef.value.validate((valid: boolean) => {
            if (valid) {
                if (state.dialog.type === constConfig.dialogType.add) {
                    processApi().save(state.ruleForm).then(() =>{
                        closeDialog();
                        emit('refresh');
                    })
                } else {
                    processApi().update(state.ruleForm).then(()=>{
                        closeDialog();
                        emit('refresh');
                    })
                }
            }
        })
    };

    // 暴露变量
    defineExpose({
        openDialog,
    });
</script>
